package code.formal.ch_02;

/**
 * NutritionFacts1类：
 * <p>
 * 共有6个属性，其中前两个是必填的，后四个是可选的。
 * <p>
 * <p>
 * 本例采用了Builder模式进行实现
 * <p>
 * <p>
 */
public class NutritionFacts4 {

    private final int servingSize;  //(ml)               必要
    private final int servings;     //(per container)    必要
    private final int calories;     //卡路里              可选
    private final int fat;          //脂肪(g)             可选
    private final int sodium;       //纳 (mg)             可选
    private final int carbohydrate; //碳水化合物 (g)       可选


    /**
     * 构造器私有
     *
     * @param builder 构建者
     */
    private NutritionFacts4(Builder builder) {
        this.servingSize = builder.servingSize;
        this.servings = builder.servings;
        this.calories = builder.calories;
        this.fat = builder.fat;
        this.sodium = builder.sodium;
        this.carbohydrate = builder.carbohydrate;
    }


    @Override
    public String toString() {
        return "NutritionFacts1{" +
                "servingSize=" + servingSize +
                ", servings=" + servings +
                ", calories=" + calories +
                ", fat=" + fat +
                ", sodium=" + sodium +
                ", carbohydrate=" + carbohydrate +
                '}';
    }


    public static class Builder {

        private final int servingSize;  //(ml)               必要
        private final int servings;     //(per container)    必要

        private int calories = 0;     //卡路里              可选
        private int fat = 0;          //脂肪(g)             可选
        private int sodium = 0;       //纳 (mg)             可选
        private int carbohydrate = 0; //碳水化合物 (g)       可选


        public Builder(int servingSize, int servings) {
            this.servingSize = servingSize;
            this.servings = servings;
        }

        public Builder setCalories(int calories) {
            this.calories = calories;
            return this;
        }

        public Builder setFat(int fat) {
            this.fat = fat;
            return this;
        }

        public Builder setSodium(int sodium) {
            this.sodium = sodium;
            return this;
        }

        public Builder setCarbohydrate(int carbohydrate) {
            this.carbohydrate = carbohydrate;
            return this;
        }


        public NutritionFacts4 build() {
            return new NutritionFacts4(this);
        }

    }


    public static void main(String[] args) {


    }


}
